#**************************************************************************** 
# 
#  Copyright(c) 2010-2011 Shenzhen TP-LINK Technologies Co.Ltd. 
#  All Rights Reserved 
#  YangXv <yangxu@tp-link.com.cn> 
# 
#***************************************************************************

# Top-level Makefile for all linux platforms 

ifndef MODEL
$(error "You have to define MODEL, such as VG3631V1")
endif

ifndef REGION
$(error "You have to define REGION, such as US")
endif

###############################################################
#			basic paths
###############################################################
#define basic variable
TOP_PATH := $(shell pwd)/..

BUILD_PATH := $(TOP_PATH)/build

COMMON_MKS:=$(BUILD_PATH)/common

#### host tools path
HOST_SRC_PATH=$(TOP_PATH)/host_tools
###############################################################
#			basic includes
###############################################################

include $(COMMON_MKS)/app_definition.mk
include $(COMMON_MKS)/module_definition.mk
include $(COMMON_MKS)/apps_target.mk
include $(COMMON_MKS)/config.mk
include $(COMMON_MKS)/fs.mk
include $(COMMON_MKS)/make.mk
include $(COMMON_MKS)/image.mk

###############################################################
#			build paths
###############################################################
#### Universal build path. All models will be placed here.
BUILD_DIR_PATH:=$(TOP_PATH)/BUILD_DIR

#### Current model build path.
PRODUCT_BUILD_PATH:=$(BUILD_DIR_PATH)/build_$(MODEL)_$(REGION)

#### Filesystem dir for target. It will be packed as squashfs or ubifs at last.
MODEL_FS_PATH:=$(PRODUCT_BUILD_PATH)/fs

#### Filesystem dir for target compiling.
OS_LIB_PATH:=$(PRODUCT_BUILD_PATH)/oslib

#### Filesystem dir for host tools, such as toolchain and filesystem making tools.
HOST_FS_PATH:=$(PRODUCT_BUILD_PATH)/host

#### Original config files path. We use CONFIG_BUILD_PATH instead.
CONFIG_PATH := $(BUILD_PATH)/config/$(MODEL)/$(REGION)

#### For the first time compiling, $(PRODUCT_BUILD_PATH)/build
#### is still NULL, and we use variables in $(CONFIG_PATH) instead.
#### After buildconfig, $(PRODUCT_BUILD_PATH)/build should be used.
#CONFIG_BUILD_PATH:=$(shell \
if [ -d "$(PRODUCT_BUILD_PATH)/build" ];then \
	echo $(PRODUCT_BUILD_PATH)/build; \
else \
	echo $(CONFIG_PATH); \
fi)
CONFIG_BUILD_PATH:=$(OS_LIB_PATH)/etc
###############################################################
#			variable includes
###############################################################
#### Trick: It force makefile execute $(PRODUCT_BUILD_PATH)/.init 
#### target first. We can do initialization here.
#### For the first time makefile is executed, $(CONFIG_BUILD_PATH)
#### has not been executed yet. Makefile will ignore it and continue
#### parsing. If a target for this file exist, the target will be executed
#### and makefile will detected that this file is updated, and the whole Makefile
#### will be reparsed.

include $(CONFIG_PATH)/variables.mk
include $(CONFIG_BUILD_PATH)/model.config

###############################################################
#			tools path
###############################################################
#### Most host tools are placed here.
HOST_TOOLS_PATH:=$(HOST_FS_PATH)/usr/bin

#### create_shadow create soft links for most files to reudce build path size.
CREATE_SHADOW:=$(HOST_TOOLS_PATH)/createShadow

#### tool chain path. For some sdks using different toolchains 
#### for kernel and uboot repectively, this toolchain is specific 
#### to kernel toolchain.
TOOLCHAIN_PATH:=$(HOST_FS_PATH)/$(TOOLCHAIN_NAME)

#### toolchain prefix. used in kernel.
TOOLPREFIX:=$(TOOLCHAIN_PATH)/$(TOOLPREFIX_NAME)

###############################################################
#			gpl path
###############################################################
#### GPL source path. If make gpl_build, all gpl source codes will
#### be generated here.
GPL_SRC_PATH = $(PRODUCT_BUILD_PATH)/gpl


###############################################################
#			variables
#DO NOT EXPORT TOO MUCH VARIABLES!!!!
###############################################################
#### For complier
CC = $(TOOLPREFIX)gcc
LD = $(TOOLPREFIX)ld
AR = $(TOOLPREFIX)ar
STRIP = $(TOOLPREFIX)strip
RANLIB = $(TOOLPREFIX)ranlib
export TOOLPREFIX
export CC
export LD
export AR
export STRIP
export RANLIB
export ARCH
export HOST
export PATH := $(TOOLCHAIN_PATH)/usr/bin:$(PATH)

#### For all
export MODEL_FS_PATH
export OS_LIB_PATH
export HOST_FS_PATH

#### for some apps using host tools.
export HOST_TOOLS_PATH

#### for model.config used by some cmm apps
export CONFIG_BUILD_PATH

#### for some apps using toolchain libs, such as libstdc++(used by ookla)
export TOOLCHAIN_PATH

#### for some app Makefiles.
export COMMON_MKS

##this value is used by pkg-config to check where is the current root path. 
##if not set, the root path of pkg-config in toolchain will be set to the 
##toolchain path.
export PKG_CONFIG_SYSROOT_DIR=/


###############################################################
#			app includes
#Shoule be placed at the last of all paths and variables
###############################################################
include $(BUILD_PATH)/app.mk


include $(HOST_SRC_PATH)/app.mk

#### SDK_SRC_PATH is specified in variables.mk.
#### At the first time variables.mk is not OK, Makefile will ignore it.
include $(SDK_SRC_PATH)/app.mk
include $(MODULES_SRC_PATH)/app.mk
#### APPS_SRC_PATH is specified in variables.mk.
#### At the first time variables.mk is not OK, Makefile will ignore it.
include $(APPS_SRC_PATH)/app.mk

include $(CONFIG_PATH)/model.mk


###############################################################
#			build environment
###############################################################
##TODO: shoule be more smart!
.PHONY:env
env:
	@-mkdir -p $(HOST_FS_PATH)
	$(call init_host_fs,$(HOST_FS_PATH))
	
	mkdir -p $(OS_LIB_PATH)
	$(call init_host_fs,$(OS_LIB_PATH))
	
	mkdir -p $(MODEL_FS_PATH)
	$(call init_fs, $(MODEL_FS_PATH))
	
	mkdir -p $(PRODUCT_BUILD_PATH)/flag
	
	mkdir -p $(GPL_SRC_PATH)
	

ifeq ($(shell [ -f $(CONFIG_BUILD_PATH)/model.config ] && echo y),)
$(CONFIG_BUILD_PATH)/model.config:env config
	touch $(CONFIG_BUILD_PATH)/model.config
endif

###############################################################
#			old targets
###############################################################
.PHONY:all clean distclean allclean

env_build:env
env_clean:all/distclean

region_build:config tools sdk/tools
region_clean:config/distclean tools/distclean sdk/tools/distclean

#boot_build:sdk/boot
boot_build:sdk/boot
boot_clean:sdk/boot/distclean

kernel_build:sdk/kernel
kernel_clean:sdk/kernel/distclean

modules_build:sdk/modules
modules_clean:sdk/modules/distclean

apps_build:apps sdk/apps
apps_clean:apps/distclean sdk/apps/distclean

fs_build:fs
fs_clean:fs/distclean

image_build:image
image_clean:image/distclean


###############################################################
#			new targets
###############################################################
### normal compile target
all:config tools sdk modules apps image
gpl:config/gpl tools/gpl sdk/gpl modules/gpl apps/gpl
	cd $(PRODUCT_BUILD_PATH) && tar -cjf image/$(MODEL)_$(REGION)_GPL.tar.bz2 gpl
	rm -rf $(GPL_SRC_PATH)/*
	
distclean:
	rm -rf $(PRODUCT_BUILD_PATH)
gplclean:
	rm -rf $(GPL_SRC_PATH)/*
#### clean all 
	

